//
//  LPHeaderDetailHeaderVIew.m
//  ZhongWangYuanApp
//
//  Created by liuyingping on 2019/1/29.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "LPHeaderDetailHeaderVIew.h"

@interface LPHeaderDetailHeaderVIew ()<YBPopupMenuDelegate>
/**    */
@property (strong, nonatomic) UIView  *timeView;
/**    */
@property (strong, nonatomic) UILabel  *timeLabel;
/**    */
@property (strong, nonatomic) NSMutableArray  *timeArray;
/**    */
@property (strong, nonatomic) UILabel  *xueshi;

@end

@implementation LPHeaderDetailHeaderVIew

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self layoutUI];
        for (int i = 0; i < 20; i ++) {
            [self.timeArray addObject:[NSString stringWithFormat:@"%ld",[NSCalendar currentYear] - i]];
        }
        [self.timeArray insertObject:@"全部" atIndex:0];
    }
    return self;
}



#pragma mark - 界面
- (void)layoutUI{
    [self addSubview:self.timeView];
    [self addSubview:self.xueshi];
    self.xueshi.hidden = YES;
    
    self.timeView.sd_layout
    .centerXEqualToView(self)
    .centerYEqualToView(self)
    .heightIs(40 * [UIScreen zj_screenHScale])
    .widthIs(160 * [UIScreen zj_screenWScale]);
    
    self.xueshi.sd_layout
    .centerYEqualToView(self)
    .rightSpaceToView(self, 20)
    .widthIs(160 * [UIScreen zj_screenWScale])
    .heightIs(40 * [UIScreen zj_screenHScale]);
    
    UILabel *nianfen = [UILabel new];
    nianfen.text = @"年份: ";
    nianfen.font = LYPFONT(16);
    nianfen.textColor = [UIColor textGrayColor];
    [self.timeView addSubview:nianfen];
    nianfen.sd_layout
    .centerYEqualToView(self.timeView)
    .leftSpaceToView(self.timeView, 15 * [UIScreen zj_screenWScale])
    .heightIs(20 * [UIScreen zj_screenHScale])
    .widthIs(40 * [UIScreen zj_screenWScale]);
    
    [self.timeView addSubview:self.timeLabel];
    self.timeLabel.sd_layout
    .centerYEqualToView(self.timeView)
    .leftSpaceToView(nianfen, 5 * [UIScreen zj_screenWScale])
    .widthIs(80 * [UIScreen zj_screenWScale])
    .heightIs(30 * [UIScreen zj_screenHScale]);
     self.timeLabel.text = @"全部";
    
    UIImageView *downImageView = [UIImageView new];
    downImageView.image = [UIImage imageNamed:@"jt_down"];
    [self.timeLabel addSubview:downImageView];
    downImageView.sd_layout
    .topEqualToView(self.timeLabel)
    .rightEqualToView(self.timeLabel)
    .bottomEqualToView(self.timeLabel)
    .widthIs(30 * [UIScreen zj_screenWScale]);
    
    UIView *under = [UIView new];
    under.backgroundColor = [UIColor themeGrayColor];
    [self addSubview:under];
    under.sd_layout.leftEqualToView(self).bottomEqualToView(self).widthIs([UIScreen zj_screenWith]).heightIs(1);
    
}


- (void)setText:(NSString *)text{
    _text = text;
    CGFloat width = ([UIScreen zj_screenWith] - self.timeView.width * 2) / 4;
    if ([text isEqualToString:@"已获学时"]) {
        self.timeView.sd_layout
        .leftSpaceToView(self, width);
        [self.timeView updateLayout];
        
        self.xueshi.text = [NSString stringWithFormat:@"已获学时: %@",self.score];
        self.xueshi.sd_layout
        .rightSpaceToView(self, width);
        [self.xueshi updateLayout];
         self.xueshi.hidden = NO;
    }
}

- (void)tapChooserTime{
   [YBPopupMenu showRelyOnView:self.timeLabel titles:self.timeArray icons:nil menuWidth:90 showCount:8 delegate:self];
}


#pragma mark - YBPopupMenuDelegate
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu{
    self.timeLabel.text = self.timeArray[index];
    NSString *chooseString = self.timeLabel.text;
    if ([chooseString isEqualToString:@"全部"]) {
        chooseString = @"";
    }
    if (self.delegate) {
        [self.delegate LPHeaderDetailHeaderVIewWithChooseTimeString:chooseString];
    }
    
}


#pragma mark - getter
- (UIView *)timeView{
    if (_timeView == nil) {
        _timeView = [UIView new];
        _timeView.backgroundColor = [UIColor whiteColor];
        
    }
    return _timeView;
}

- (UILabel *)timeLabel{
    if (_timeLabel == nil) {
        _timeLabel = [UILabel new];
        _timeLabel.layer.borderColor = [UIColor themeGrayColor].CGColor;
        _timeLabel.layer.borderWidth = 1;
        _timeLabel.textColor = [UIColor textGrayColor];
        _timeLabel.font = LYPFONT(15);
        _timeLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapChooserTime)];
        [_timeLabel addGestureRecognizer:tap];
    }
    return _timeLabel;
}

- (UILabel *)xueshi{
    if (_xueshi == nil) {
        _xueshi = [UILabel new];
        _xueshi.text = [NSString stringWithFormat:@"已获学时: 0"];
        _xueshi.font = LYPFONT(15);
        _xueshi.textColor = [UIColor textGrayColor];
        _xueshi.textAlignment = NSTextAlignmentCenter;
    }
    return _xueshi;
}

- (NSMutableArray *)timeArray{
    if (_timeArray == nil) {
        _timeArray = [NSMutableArray array];
    }
    return _timeArray;
}
@end
